package com.software.servlet;

import com.software.model.Product;
import com.software.service.ProductService;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ProductServlet extends HttpServlet {

    private ProductService productService;

    @Override
    public void init() throws ServletException {
        // 初始化 ProductService
        productService = new ProductService();
    }

    // 处理 GET 请求（获取商品列表）
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        // 如果 action 为 "list" 则返回所有商品
        if ("list".equals(action)) {
            List<Product> productList = productService.getAllProducts();
            request.setAttribute("productList", productList);
            // 转发到商品列表页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/displayProduct.jsp");
            dispatcher.forward(request, response);
        }
        // 如果 action 为 "details" 则返回指定商品详情
        else if ("details".equals(action)) {
            String productId = request.getParameter("productId");
            Product product = productService.getProductById(productId);
            request.setAttribute("product", product);
            // 转发到商品详情页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/displayProduct.jsp");
            dispatcher.forward(request, response);
        }
        // 如果 action 为 "recommended" 则返回推荐商品
        else if ("recommended".equals(action)) {
            List<Product> recommendedProducts = productService.getRecommendedProducts();
            request.setAttribute("recommendedProducts", recommendedProducts);
            // 转发到推荐商品页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/recommended-products.jsp");
            dispatcher.forward(request, response);
        }
    }

    // 处理 POST 请求（添加商品或批量删除）
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("add".equals(action)) {
            // 获取请求参数并创建 Product 对象
            String productId = request.getParameter("productId");
            String productName = request.getParameter("productName");
            Double productPrice = Double.valueOf(request.getParameter("productPrice"));
            String productDesc = request.getParameter("productDesc");
            String productImage = request.getParameter("productImage");
            Integer typeId = Integer.valueOf(request.getParameter("typeId"));
            Double quantity = Double.valueOf(request.getParameter("quantity"));
            Boolean recommend = Boolean.valueOf(request.getParameter("recommend"));

            Product product = new Product(productId, productName, productPrice, productDesc, productImage, typeId, quantity, recommend);

            // 调用 ProductService 添加商品
            productService.addProduct(product);

            // 重定向到商品列表页
            response.sendRedirect(request.getContextPath() + "/product?action=list");
        }
        else if ("batchDelete".equals(action)) {
            // 批量删除操作
            String[] productIds = request.getParameterValues("productid");
            if (productIds != null) {
                for (String productId : productIds) {
                    // 调用 ProductService 删除商品
                    productService.deleteProduct(productId);
                }
            }

            // 重定向到商品列表页
            response.sendRedirect(request.getContextPath() + "/product?action=list");
        }
    }

    // 处理 PUT 请求（更新商品信息）
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("update".equals(action)) {
            // 获取请求参数并创建 Product 对象
            String productId = request.getParameter("productId");
            String productName = request.getParameter("productName");
            Double productPrice = Double.valueOf(request.getParameter("productPrice"));
            String productDesc = request.getParameter("productDesc");
            String productImage = request.getParameter("productImage");
            Integer typeId = Integer.valueOf(request.getParameter("typeId"));
            Double quantity = Double.valueOf(request.getParameter("quantity"));
            Boolean recommend = Boolean.valueOf(request.getParameter("recommend"));

            Product product = new Product(productId, productName, productPrice, productDesc, productImage, typeId, quantity, recommend);

            // 调用 ProductService 更新商品
            productService.updateProduct(product);

            // 重定向到商品列表页
            response.sendRedirect(request.getContextPath() + "/product?action=list");
        }
    }

    // 处理 DELETE 请求（删除商品）
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String productId = request.getParameter("productId");

        if (productId != null && !productId.isEmpty()) {
            // 调用 ProductService 删除商品
            productService.deleteProduct(productId);

            // 重定向到商品列表页
            response.sendRedirect(request.getContextPath() + "/product?action=list");
        }
    }
}
